package com.macro.mall.ums.service.login.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.ums.domain.MemberLoginLog;
import com.macro.mall.ums.entity.MemberLoginLogEntity;
import com.macro.mall.ums.mapper.MemberLoginLogMapper;
import com.macro.mall.ums.service.login.MemberLoginLogService;
import com.macro.mall.ums.service.login.bo.InsertMemberLoginLogInput;
import com.macro.mall.ums.service.login.bo.PagedQueryMemberLoginLogInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员登录记录Service
 *
 * @author xuyanjun
 * @date 2019-12-09
 */
@Slf4j
@Service
public class MemberLoginLogServiceImpl implements MemberLoginLogService {

    @Autowired
    private MemberLoginLogMapper memberLoginLogMapper;

    /**
     * 新增会员登录记录
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertMemberLoginLogInput input) {
        MemberLoginLogEntity memberLoginLogEntity = new MemberLoginLogEntity();
        memberLoginLogEntity
                .setMemberId(input.getMemberId())
                .setIp(input.getIp())
                .setProvince(input.getProvince())
                .setCity(input.getCity())
                .setLoginType(input.getLoginType())
                .setSourceType(input.getSourceType())
                .setAgent(input.getAgent());

        LocalDateTime now = LocalDateTime.now();
        memberLoginLogEntity.setCreateTime(now);

        memberLoginLogMapper.insert(memberLoginLogEntity);
        return memberLoginLogEntity.getId();
    }

    /**
     * 删除会员登录记录
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        memberLoginLogMapper.deleteById(id);
    }

    /**
     * 根据Id获取会员登录记录
     *
     * @param id id
     * @return 会员登录记录
     */
    @Override
    public MemberLoginLogEntity getById(long id) {
        return memberLoginLogMapper.selectById(id);
    }

    /**
     * 根据批量Id获取会员登录记录
     *
     * @param batchIds 批量ids
     * @return 会员登录记录集合
     */
    @Override
    public List<MemberLoginLogEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return memberLoginLogMapper.selectBatchIds(batchIds);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的会员登录记录
     */
    @Override
    public Page<MemberLoginLogEntity> pagedQuery(PagedQueryMemberLoginLogInput input) {
        LambdaQueryWrapper<MemberLoginLogEntity> queryWrapper = Wrappers.lambdaQuery();

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<MemberLoginLogEntity>) memberLoginLogMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param memberLoginLogEntity 实体
     * @return memberLoginLog
     */
    @Override
    public MemberLoginLog entityToDomain(MemberLoginLogEntity memberLoginLogEntity) {
        if (memberLoginLogEntity == null) {
            return null;
        }

        MemberLoginLog memberLoginLog = new MemberLoginLog();
        memberLoginLog
                .setId(memberLoginLogEntity.getId())
                .setCreateTime(memberLoginLogEntity.getCreateTime())
                .setMemberId(memberLoginLogEntity.getMemberId())
                .setIp(memberLoginLogEntity.getIp())
                .setProvince(memberLoginLogEntity.getProvince())
                .setCity(memberLoginLogEntity.getCity())
                .setLoginType(memberLoginLogEntity.getLoginType())
                .setSourceType(memberLoginLogEntity.getSourceType())
                .setAgent(memberLoginLogEntity.getAgent());

        return memberLoginLog;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param memberLoginLogEntities 实体
     * @return memberLoginLogs
     */
    @Override
    public List<MemberLoginLog> entityToDomain(List<MemberLoginLogEntity> memberLoginLogEntities) {
        List<MemberLoginLog> result = new ArrayList<>();
        if (memberLoginLogEntities == null) {
            return result;
        }

        for (MemberLoginLogEntity memberLoginLogEntity : memberLoginLogEntities) {
            MemberLoginLog memberLoginLog = entityToDomain(memberLoginLogEntity);
            result.add(memberLoginLog);
        }
        return result;
    }

}
